<template>
    <baseView>

        <div class="reset-wrap">
            <div class="reset-form">
                <div class="reset-wrap">
                    <div class="title">修改密码</div>
                    <!-- 登录弹框 -->
                    <div class="form-wrap">
                        <el-form :model="form" :rules="rules" ref="resetForm">
                            <el-form-item prop="password1">
                                <el-input class="reset-input" v-model="form.password1" placeholder="请输入密码"
                                    type="password" show-password>
                                    <template #prepend>
                                        <img :src="password" alt="">
                                    </template>
                                </el-input>
                            </el-form-item>
                            <el-form-item prop="password2">
                                <el-input class="reset-input" v-model="form.password2" placeholder="请再次输入密码"
                                    type="password" show-password>
                                    <template #prepend>
                                        <img :src="password" alt="">
                                    </template>
                                </el-input>
                            </el-form-item>
                        </el-form>
                        <el-button class="reset-input" type="primary" @click="submit">修改密码</el-button>
                    </div>
                </div>
            </div>
        </div>

    </baseView>
</template>

<script setup>
import password from "@/assets/images/password.png"
import { resetPass } from "@/api/sys"
import { logoutApi } from "@/api/user"
import { useUser } from "@/store/userStore"
const router = useRouter()
const form = reactive({
    password1: "",
    password2: ""
})
const resetForm = ref(null)
function validatePass2(rules, value, callback) {
    if (value == '') {
        callback(new Error('请再次输入密码'))
    } else if (form.password1 != form.password2) {
        callback(new Error('两次密码输入不一致'))
    } else {
        callback()
    }
}
function validatePass(rule, value, callback) {
    if (value === '') {
        callback(new Error('请输入密码'))
    } else {
        if (form.password2 !== '') {
            if (!resetForm.value) return
            resetForm.value.validateField('password2')
        }
        callback()
    }
}
const rules = reactive({
    password1: [{ trigger: 'blur', validator: validatePass }],
    password2: [{ trigger: 'blur', validator: validatePass2 }]
})

const userStore = useUser()
function submit() {
    resetForm.value.validate((valid, fields) => {
        if (valid) {
            resetPass({ newPwd: form.password1 }).then(res => {
                if (res.code == 200) {
                    logoutApi().then(res => {
                        if (res.code == 200) {
                            userStore.logout()
                            ElMessage.success("修改成功，请重新登录！")
                            router.push("/login")
                        }
                    })
                }
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}
</script>

<style lang="scss" scoped>
.reset-wrap {
    height: 100%;
    width: 100%;
    position: relative;

    .reset-form {
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        width: 400px;
        height: 360px;
        background: rgba(255, 255, 255, 0.7);
        box-shadow: 0px 2px 20px rgba(54, 76, 102, 0.25);
        backdrop-filter: blur(20px);

        .reset-wrap {
            height: 100%;
            width: 100%;
            display: flex;
            flex-direction: column;
            // justify-content: space-around;
            // align-items: center;
            // justify-content: center;
            align-items: center;

            .title {
                margin: 24px 0 41px 0;
                font-size: 24px;
                letter-spacing: 2px;
                color: rgba(126, 138, 148, 1);
            }

        }

    }
}

.reset-input {
    width: 280px;
    height: 44px;
}
</style>